package BLC

import (
	"flag"
	"fmt"
	"os"
)

type Client struct {
	BC *BlockChain
}

func PrintUsage() {
	fmt.Println("Usage:")
	fmt.Printf("\tsendCmd -from FROM -to TO  -amount AMOUNT  -- 转账 .\n")
	fmt.Printf("\tprintChainCmd -- 输出区块链信息.\n")
	fmt.Printf("\tcreateBlockChainCmd -addr ADDR -- 地址.\n")
	fmt.Printf("\tgetBalanceCmd -from FROM --查询余额 \n")
}

func IsValidArgs() bool {
	if len(os.Args) < 2 {
		PrintUsage()
		//os.Exit(1)
		return false
	}

	return true
}

//运行函数
func (cli *Client) Run() {
	if IsValidArgs() {
		printChainCmd := flag.NewFlagSet("print chain", flag.ExitOnError)
		//创建区块
		createBICWithGenesisCmd := flag.NewFlagSet("create block chain", flag.ExitOnError)
		//发送交易
		SendCmd := flag.NewFlagSet("send swap", flag.ExitOnError)
		//查询余额
		getBalance := flag.NewFlagSet("get balance", flag.ExitOnError)

		cli.OsArgs(SendCmd, printChainCmd, createBICWithGenesisCmd, getBalance)
	}

}

func (cli *Client) OsArgs(SendCmd, printChainCmd, createBICWithGenesisCmd, getBalance *flag.FlagSet) {
	switch os.Args[1] {
	case "sendCmd":
		cli.sendCmd(SendCmd)
	case "printChainCmd":
		cli.printChainCmd(printChainCmd)
	case "createBlockChainCmd":
		cli.createBlockChainCmd(createBICWithGenesisCmd)
	case "getBalanceCmd":
		cli.getBalanceCmd(getBalance)
	default:
		PrintUsage()
		os.Exit(1)
	}
}
